package com.company.beauty.ids.handler;

import cn.hutool.extra.spring.SpringUtil;

import java.util.Comparator;
import java.util.List;

/**
 * <p>snowflake handler factory</p >
 *
 * @author youyoukele
 * @version 1.0
 * @date 2024/4/17 11:07
 */
public class SnowflakeHandlerFactory {

    /**
     * 加载处理集合
     */
    private static final List<SnowflakeHandler> BEANS = SpringUtil.getBeansOfType(SnowflakeHandler.class).values()
            .stream()
            // 按照权重排序
            .sorted(Comparator.comparingInt(SnowflakeHandler::weight).reversed())
            .toList();

    /**
     * 默认处理
     */
    private static final SnowflakeHandler DEFAULT_BEAN = SpringUtil.getBean(SnowflakeRedisHandler.class);

    /**
     * get snowflake handler
     *
     * @return
     */
    public static SnowflakeHandler getSnowflakeHandler() {
        return BEANS.stream().findFirst().orElse(DEFAULT_BEAN);
    }

}
